package com.moonstar;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.*;

import static com.moonstar.ProcessAClient.MEMORYFILE_NAME;
import static com.moonstar.ProcessAClient.PIPEFILE_NAME;

public class ProcessBThread implements Runnable{


    @Override
    public void run() {
        //当线程b被创建时，先绘制进程B的界面
        JFrame jframe = new JFrame("进程B");
        jframe.setBounds(0,0,300,400);
        jframe.setLayout(new FlowLayout(FlowLayout.CENTER));

        //JTextArea消息框
        JTextArea jTextArea = new JTextArea(10, 20);
        //自动换行
        jTextArea.setLineWrap(true);


        //jScrollPane用于装载text消息框
        JScrollPane jScrollPane = new JScrollPane(jTextArea);
        jScrollPane.setSize(250, 200);

        //构建"从内存映像文件读数据"按钮
        JButton inFromMemoryFileButton = new JButton("从内存映像文件读数据");
        inFromMemoryFileButton.setSize(200, 20);
        inFromMemoryFileButton.addActionListener(new AbstractAction() {
            //当inFromMemoryFileButton被点击的时候,从内存映像文件中将数据读取并且显示到text框中
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    String data=new String();
                    FileInputStream fin = new FileInputStream(new File(MEMORYFILE_NAME));
                    byte[] bytes = new byte[1024];
                    int len;
                    while(((len=fin.read(bytes))!=-1)){
                        data=data+new String(bytes,0,len);
                    }
                    jTextArea.setText(data);
                    fin.close();
                } catch (FileNotFoundException ex) {
                    ex.printStackTrace();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }

            }
        });

        //构建"创建管道并接收数据"按钮
        JButton createAndReciveFromPipeButton = new JButton("创建管道并接收数据");
        createAndReciveFromPipeButton.setSize(200, 20);
        createAndReciveFromPipeButton.addActionListener(new AbstractAction() {
            //当createAndReciveFromPipeButton被点击的时候,创建管道文件，并且从中接收数据
            @Override
            public void actionPerformed(ActionEvent e) {
                //直接创建并且接收数据
                try {
                    File file = new File(PIPEFILE_NAME);
                    if(!file.exists()){
                       //如果不存在，直接创建
                        file.createNewFile();
                    }
                    String data=new String();
                    FileInputStream fin=new FileInputStream(new File(PIPEFILE_NAME));
                    byte[] bytes = new byte[1024];
                    int len;
                    while((len=fin.read(bytes))!=-1){
                        data=data+new String(bytes,0,len);
                    }
                    jTextArea.setText(data);
                } catch (FileNotFoundException ex) {
                    ex.printStackTrace();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        });


        //构建退出窗口按钮
        JButton exitButton = new JButton("退出");
        exitButton.setSize(70,20);
        exitButton.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //删除管道文件，如果存在。
                File file = new File(PIPEFILE_NAME);
                if(file.exists()){
                    //删除文件
                    file.delete();
                }
                //关闭窗口并且让进程A中的标识为设置为false;
                ProcessAClient.isCreateB=false;
                jframe.dispose();
            }
        });



        //jFrame装填各种组件
        jframe.add(jScrollPane);
        jframe.add(inFromMemoryFileButton);
        jframe.add(createAndReciveFromPipeButton);
        jframe.add(exitButton);

//        jframe.setResizable();   //最后确定窗口大小
        jframe.setVisible(true);

        jframe.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                ProcessAClient.isCreateB=false;
                //删除管道文件，如果存在。
                File file = new File(PIPEFILE_NAME);
                if(file.exists()){
                    //删除文件
                    file.delete();
                }
                super.windowClosing(e);
            }
        });

        //不设置强制退出界面，使用退出界面按钮代替
        jframe.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);



        //调出一个线程去监听线程A的SendMessage的动作
        Thread saveMessageThread = new Thread(new SaveMessageThread(jTextArea));
        saveMessageThread.start();

    }


}
